﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class TheLoaiDAO:AbstractDAO
    {
        public static List<TheLoaiDTO> LayDSTheLoai()
        {
            // Mo ket noi va tao ra danh sach the loai
            OleDbConnection connect = null;
            List<TheLoaiDTO> dsTheLoai = new List<TheLoaiDTO>();

            try
            {
                connect = MoKetNoi();
                string chuoiLenh = "SELECT ID,MaTheLoai,TenTheLoai FROM [THE LOAI]";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, connect);
                OleDbDataReader boDoc = lenh.ExecuteReader();

                while(boDoc.Read())
                {
                    TheLoaiDTO TheLoai = new TheLoaiDTO();
                    if (boDoc.GetInt32(0) >= 0)
                        TheLoai.TheLoaiID = boDoc.GetInt32(0);
                    if (!boDoc.IsDBNull(1))
                        TheLoai.MaTheLoai = boDoc.GetString(1);
                    if (!boDoc.IsDBNull(2))
                        TheLoai.TenTheLoai = boDoc.GetString(2);
                    dsTheLoai.Add(TheLoai);
                } // ket thuc while
            } // ket thuc try
            catch(Exception ex)
            {
                dsTheLoai = new List<TheLoaiDTO>();
            } // ket thuc catch
            finally
            {
                if (connect != null && connect.State == System.Data.ConnectionState.Open)
                    connect.Close();
            } // ket thuc finally

            // tra ve danh sach the loai
            return dsTheLoai;
        } // ket thuc phuong thuc LayDSTheLoai
    } // ket thuc class TheLoaiDAO
}
